/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.hohai.river.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hohai.river.constants.Constants;
import com.hohai.river.entity.River;
import com.hohai.river.entity.SysRegion;
import com.hohai.river.mapper.SysRegionMapper;
import com.hohai.river.service.RiverService;
import com.hohai.river.service.SysRegionService;
import com.hohai.river.utils.Node;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 地区表
 *
 * @author pig code generator
 * @date 2022-09-14 14:42:30
 */
@Service
public class SysRegionServiceImpl extends ServiceImpl<SysRegionMapper, SysRegion> implements SysRegionService {

	@Autowired
	private RiverService riverService;

	@Override
	public Map getCascadeData(Long pid, Integer level, String year) {
		List<SysRegion> regions = this.baseMapper.selectByRegionParentId(pid);
		Map<String, Object> optionData = new HashMap<>();
		List<Long> cityIds = new ArrayList<>();
		List<Long> countyIds = new ArrayList<>();

		// 城市列表
		List<Node> cityList = new ArrayList<>();
		// 城市ID -> 区县列表
		Map<Long, List<Node>> countyMap = new HashMap<>();
		// 区县ID -> 河湖列表
		Map<Long, List<Node>> riverMap = new HashMap<>();

		regions.forEach(region -> {
			cityList.add(new Node(region.getRegionId(), region.getRegionName()));
			countyMap.put(region.getRegionId(), new ArrayList<>());
			cityIds.add(region.getRegionId());
		});
		optionData.put("city", cityList);

		List<SysRegion> counties = this.baseMapper.getCountyByCityIds(cityIds);
		counties.forEach(region -> {
			countyMap.get(region.getRegionParentId()).add(new Node(region.getRegionId(), region.getRegionName()));
			riverMap.put(region.getRegionId(), new ArrayList<>());
			countyIds.add(region.getRegionId());
		});
		optionData.put("county", countyMap);

		Stream<River> riverStream = this.riverService.selectByCountyIds(countyIds).stream();
		if (level != null) {
			riverStream = riverStream.filter(r -> r.getLevel().equals(level));
		}
		if (!StringUtils.isEmpty(year)) {
			riverStream = riverStream.filter(r -> r.getYear().equals(year));
		}
		riverStream.forEach(r -> {
			riverMap.get(r.getCountyId()).add(new Node(r.getId(), r.getName()));
		});
		optionData.put("river", riverMap);

		return optionData;
	}

	@Override
	public List<Node> getSelectList(Long pid) {
		List<SysRegion> regions = this.baseMapper.selectByRegionParentId(pid);
		List<Node> list = new ArrayList<>();
		regions.forEach(region -> {
			list.add(new Node(region.getRegionId(), region.getRegionName()));
		});
		return list;
	}

}
